# skycalc_classes.py
#
# This file is a helper to the automatically generated skycalc.py
#

import skycalc
from ..astrodatetime import datetime
from datetime import datetime as py_datetime

class Almanac(object):
    def __init__(self, datetimeObj=datetime.now(), **kwargs):
        """
        
        """
        
        if kwargs.has_key("site"):
            site = kwargs['site']
            latitude = site.latitude
            longitude = site.longitude
            elevation = site.elevation
            horizon = site.horizon
            timezone = site.timeZone
            dst = site.dst

        else:
            if kwargs.has_key("latitude"):
                try:
                    latitude = float(kwargs["latitude"])
                except ValueError:
                    raise ValueError("Error parsing latitude! %s" % kwargs["latitude"])
            else:
                raise ValueError("If you don't provide a Site object, you must specify at least latitude and longitude!")
            
            if kwargs.has_key("longitude"):
                try:
                    longitude = float(kwargs["longitude"])
                except ValueError:
                    raise ValueError("Error parsing longitude! %s" % kwargs["longitude"])
            else:
                raise ValueError("If you don't provide a Site object, you must specify at least latitude and longitude!")
                
            if kwargs.has_key("elevation"):
                try:
                    elevation = float(kwargs["elevation"])
                except ValueError:
                    raise ValueError("Error parsing elevation! %s" % kwargs["elevation"])
            else:
                elevation = 0.0
            
            if kwargs.has_key("horizon"):
                try:
                    horizon = float(kwargs["horizon"])
                except ValueError:
                    raise ValueError("Error parsing horizon! %s" % kwargs["horizon"])
            else:
                horizon = 0.0
            
            if kwargs.has_key("timezone"):
                try:
                    timezone = float(kwargs["timezone"])
                except ValueError:
                    raise ValueError("Error parsing timezone! %s" % kwargs["timezone"])
            else:
                timezone = round((py_datetime.utcnow() - py_datetime.now()).total_seconds()/60./60.)
            
            if kwargs.has_key("dst"):
                try:
                    dst = float(kwargs["dst"])
                except ValueError:
                    raise ValueError("Error parsing dst! %s" % kwargs["dst"])
            else:
                dst = 0
        
        # if not isinstance(site, whatever.Site()):
        
        # If the input datetime object is not an astrodatetime.datetime object,
        #   convert it to one. If it
        if isinstance(datetimeObj, py_datetime):
            datetimeObj = datetime.fromDatetime(datetimeObj)
        elif not isinstance(datetimeObj, py_datetime) and not isinstance(datetimeObj, datetime):
            raise ValueError("Invalid datetime object! Can be either an Astrodatetime.datetime or datetime.datetime object.")

        if kwargs.has_key('mjd'):
            try:
                datetimeObj = datetime.fromMJD(float(kwargs['mjd']))
            except:
                raise ValueError("Error parsing MJD input. %s" % kwargs['mjd'])
        
        if kwargs.has_key('jd'):
            try:
                datetimeObj = datetime.fromMJD(float(kwargs['jd']))
            except:
                raise ValueError("Error parsing JD input. %s" % kwargs['jd'])
                
        
        
        # If site is none, attempt geolocation?
        #if site == None:
        #    raise ValueError("You must specify a Site object using Almanac(site=___)!")
        
        dt = skycalc.date_time()
        dt.y = datetimeObj.year
        dt.mo = datetimeObj.month
        dt.d = datetimeObj.day
        dt.h = datetimeObj.hour
        dt.mn = datetimeObj.minute
        dt.s = datetimeObj.second
        
        print skycalc.almanac(dt, latitude, \
                                      longitude, \
                                      elevation, \
                                      elevation, \
                                      horizon, \
                                      timezone, \
                                      dst)

if __name__ == "__main__":
    print __package__
    # Tests
    alm = Almanac()